﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tetris
{
    /// <summary>
    /// 生成类Block
    /// </summary>
    public class Block
    {
        //定义方块的大小属性：Width,Height
        /// <summary>
        /// 定义属性Width，表明方块的宽度
        /// </summary>
        private int width;
        public int Width
        {
            get { return width; }
            set { width = value; }
        }
        /// <summary>
        /// 定义属性Height，表明方块的高度
        /// </summary>
        private int height;
        public int Height
        {
            get { return height; }
            set { height = value; }
        }
        //方块的形状属性：shapeBlock,block
        /// <summary>
        /// 定义字段shapeBlock，表明方块的形状
        /// </summary>
        private readonly int shapeBlock;
        /// <summary>
        /// 定义字段block，表明在当前位置的方块子区域的当前状态，0表明无图片状态，1表明有图片状态
        /// </summary>
        public int[,] block;
        //方块的位置属性：Top,Left
        /// <summary>
        /// 定义属性Top，表明方块到游戏区域上顶部的距离
        /// </summary>
        private int top;
        public int Top
        {
            get { return top; }
            set { top = value; }
        }
        /// <summary>
        /// 定义属性Left，表明方块到游戏区域左边缘的距离
        /// </summary>
        private int left;
        public int Left
        {
            get { return left; }
            set { left = value; }
        }
        /// <summary>
        /// 定义构造函数Block()，初始化方块
        /// </summary>
        public Block() 
        {
            //实例化伪随机数生成器Random类，生成对象随机形状方块生成器randomGenerator
            Random randomGenerator = new Random();
            //调用随机形状方块生成器randomGenerator的.Next()方法，生成随机整数randomBlock
            int randomShape = randomGenerator.Next(1, 5);
            //生成随机的方块形状
            shapeBlock = randomShape;
            switch (shapeBlock)
            {
                //I
                case 1: 
                    Width = 4;
                    Height = 1;
                    Top = 0;
                    Left = 3;
                    block = new int[Width, Height];
                    block[0, 0] = 1;
                    block[1, 0] = 1;
                    block[2, 0] = 1;
                    block[3, 0] = 1;
                    break;
                //O
                case 2:
                    Width = 2;
                    Height = 2;
                    Top = 0;
                    Left = 4;
                    block = new int[Width, Height];
                    block[0, 0] = 1;
                    block[0, 1] = 1;
                    block[1, 0] = 1;
                    block[1, 1] = 1;
                    break;
                //Ｔ
                case 3: 
                    Width = 3;
                    Height = 3;
                    Top = 0;
                    Left = 4;
                    block = new int[Width, Height];
                    block[0, 0] = 1;
                    block[1, 0] = 1;
                    block[2, 0] = 1;
                    block[1, 1] = 1;
                    block[1, 2] = 1;
                    break;
                //L
                case 4: 
                    Width = 2;
                    Height = 3;
                    Top = 0;
                    Left = 4;
                    block = new int[Width, Height];
                    block[0, 0] = 1;
                    block[0, 1] = 1;
                    block[0, 2] = 1;
                    block[1, 2] = 1;
                    break;
            }
        }
        /// <summary>
        /// 绘制在相应位置的相应形状的方块
        /// </summary>
        /// <param name="g"></param>
        public void Draw(Graphics g)
        {
            //实例化Image类生成对象方块子区域的图片blockImage
            Image blockImage = Properties.Resources.block;
            //绘制当前状态为有图片的方块子区域
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    //判断方块子区域的状态是否为有图片状态
                    if (block[i, j] == 1) 
                    {
                        //实例化Rectangle类生成方块的子区域的长方形对象recblock
                        Rectangle recblock = new Rectangle
                        {
                            Location = new Point((Left + i) * Game.blockImageWidth, (Top + j) * Game.blockImageHeight)
,
                            Size = new Size(Game.blockImageWidth, Game.blockImageHeight)
                        };
                        //绘制
                        g.DrawImage(blockImage, recblock);
                    }
                }
            }
        }
    }
}

